<?php


namespace App\Internals;


use App\Exceptions\InternalServiceException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class StudyInternal
{
    public static function createCourse($userId, $orderId, $courses)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses";
            $response = $client->request('POST', $rpcUrl, [
                'json' => [
                    'userId' => $userId,
                    'orderId' => $orderId,
                    'courses' => $courses
                ]
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getCurrentCourse($userId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.study') . "/internal/courses/current";
            $response = $client->request('GET', $rpcUrl, [
                'query' => [
                    'userId' => $userId,
                ],
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }
}
